<template>
  <div class="app-container">
    <el-row :gutter="30">
      <el-col :span="24">
        <div style="margin-top:10px">
          <!--<el-button icon="el-icon-plus" type="primary" size="mini" @click="mergeData">合并</el-button>-->
          <el-button icon="el-icon-close" type="danger" size="mini" @click="deleteAny">删除</el-button>
        </div>
        <div class="query-form">
          <el-form ref="tableSearchParam" :model="tableSearchParam" label-width="50px" :inline="true">
            <el-form-item label="公司名称" label-width="70px">
              <el-input v-model="tableSearchParam.companyName" style="width:200px" size="small" clearable></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="listTableData" icon="el-icon-search" size="small">查询</el-button>
            </el-form-item>
          </el-form>
        </div>
        <br/>
        <el-table
          :data="tableData"
          border
          empty-text="暂无数据"
          style="width: 100%"
          @selection-change="handleSelectionChange">
          <el-table-column
            type="selection"
            width="35">
          </el-table-column>
          <el-table-column
            label="公司名称"
            property="companyName">
          </el-table-column>
          <el-table-column
            label="所在省份"
            width="70">
            <template slot-scope="scope">
              <span>{{scope.row.region.split(',')[0]}}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="所在城市"
            width="70">
            <template slot-scope="scope">
              <span>{{scope.row.region.split(',')[1]}}</span>
            </template>
          </el-table-column>
          <!--<el-table-column-->
          <!--label="加入来源"-->
          <!--width="130">-->
          <!--<template slot-scope="scope">-->
          <!--<span>{{scope.row.companyFrom == '0' ? '救援公司负责人认证' : '救援司机认证'}}</span>-->
          <!--</template>-->
          <!--</el-table-column>-->
          <el-table-column
            label="负责人姓名"
            property="employeeName"
            width="90">
          </el-table-column>
          <el-table-column
            label="负责人手机"
            property="telphone"
            width="100">
          </el-table-column>
          <!--<el-table-column-->
          <!--label="司机数量"-->
          <!--property="driverNum"-->
          <!--width="70">-->
          <!--</el-table-column>-->
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                icon="el-icon-edit"
                type="success"
                @click="editData(scope.$index, scope.row)">编辑
              </el-button>
              <!--<el-button-->
              <!--size="mini"-->
              <!--icon="el-icon-lemone-user_role"-->
              <!--@click="editManager(scope.$index, scope.row)">变更负责人-->
              <!--</el-button>-->
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-close"
                @click="handleDelete(scope.$index, scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="page">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page=this.currentPage
            :page-sizes="[10, 20, 30, 40]"
            :page-size=this.pageSize
            layout="total, sizes, prev, pager, next, jumper"
            :total=this.total>
          </el-pagination>
        </div>
      </el-col>
    </el-row>
    <el-dialog title="修改公司信息" :visible.sync="dialogFormVisible" top="10vh" width="70%">
      <div style="height:400px;overflow-y: auto">
        <el-form :model="formData" :rules="rules" ref="ruleForm" style="margin-left: 20px">
          <el-row>
            <el-col :span="4" class="m-col">公司名称</el-col>
            <el-col :span="20" class="m-col-left">
              <el-input v-model="formData.companyName" auto-complete="off" style="width:255px"></el-input>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="4" class="m-col">负责人姓名</el-col>
            <el-col :span="8" class="m-col-left">{{formData.employeeName}}</el-col>
            <el-col :span="4" class="m-col-left">负责人手机</el-col>
            <el-col :span="8" class="m-col-left"> <span>{{formData.telphone}}</span></el-col>
          </el-row>
          <el-row>
            <el-col :span="4" class="m-col">所在区域</el-col>
            <el-col :span="20" class="m-col-left">
              <el-cascader
                size="small"
                style="width:255px"
                :options="options"
                v-model="selectedOptions"
                @change="handleChange">
              </el-cascader>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="4" class="m-col-bottom" style=" border-top: 1px solid #ebeef5;">营业执照</el-col>
            <el-col :span="20" class="m-col-bottom-left" style=" border-top: 1px solid #ebeef5;">
              <div style="margin-top: 25px" v-if="formData.businessLicense!=null">
                <div style="width: 100px;height: 100px" @click="showImg(formData.businessLicense.url)">
                  <img :src="formData.businessLicense==null?'':baseUrl+formData.businessLicense.url"
                       style="width:100%;height:100%"></img>
                </div>
              </div>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="4" class="m-col-bottom" style="height:350px">身份证</el-col>
            <el-col :span="20" class="m-col-bottom-left" style="height:350px">
              <div style="margin-top: 25px" v-for="item in formData.idCards">
                <div style="width: 100px;height: 100px;float:left;both:clear;margin-left:20px" @click="showImg(item.url)">
                  <img :src="baseUrl+item.url"
                       style="width:100%;height:100%;"></img>
                </div>
              </div>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="4" class="m-col-bottom">负责人照片</el-col>
            <el-col :span="20" class="m-col-bottom-left">
              <div style="margin-top: 25px">
                <div style="width: 100px;height: 100px" @click="showImg(formData.applicants.url)">
                  <img :src="formData.applicants==null?'':baseUrl+formData.applicants.url"
                       style="margin-left:20px;width:100%;height:100%;"></img>
                </div>
              </div>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleEdit" :loading="loading">保存</el-button>
      </div>
    </el-dialog>

    <el-dialog title="变更负责人" :visible.sync="dialogManagerFormVisible" top="10vh">
      <el-form :model="formManagerData" style="margin-left: 20px" :rules="rulesManager" ref="ruleManagerForm">
        <el-form-item label="负责人" :label-width="formLabelWidth" prop="newManagerId">
          <el-select v-model="formManagerData.newManagerId" placeholder="请选择" no-data-text="暂无数据" style="width:255px"
                     @change="managerSelectChange" filterable>
            <el-option
              v-for="item in allDriverData"
              :key="item.id"
              :label="item.employeeName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手机号" :label-width="formLabelWidth">
          <el-input v-model="formManagerData.telphone" auto-complete="off" style="width:255px" disabled></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogManagerFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleManager" :loading="loading">保存</el-button>
      </div>
    </el-dialog>
    <el-dialog title="查看图片" :visible.sync="dialogImgFormVisible" top="10vh">
      <div style="text-align: center">
        <img :src="baseUrl+imgUrl" style="width:80%;height: auto">
      </div>
      <div slot="footer" class="dialog-footer">

      </div>
    </el-dialog>
  </div>
</template>

<style scoped>

  .page {
    text-align: center;
    margin-top: 20px;
  }

  .query-form {
    border-bottom: 1px solid #ebeef5;
    padding-top: 25px;
  }

  .m-col {
    border: 1px solid #ebeef5;
    border-bottom: none;
    padding-left: 10px;
    height: 40px;
    line-height: 40px;
    color: #909399;
    font-weight: 500;
  }

  .m-col-left {
    border: 1px solid #ebeef5;
    border-left: none;
    border-bottom: none;
    padding-left: 10px;
    height: 40px;
    line-height: 40px;
  }

  .m-col-bottom {
    border: 1px solid #ebeef5;
    border-top: none;
    padding-left: 10px;
    height: 250px;
    line-height: 250px;
    color: #909399;
    font-weight: 500;
  }

  .m-col-bottom-left {
    border: 1px solid #ebeef5;
    border-top: none;
    border-left: none;
    padding-left: 10px;
    height: 250px;
    line-height: 250px;
  }

</style>

<script>

  import insuranceServiceSupplierApi from '@/api/insuranceServiceSupplierApi'
  import insuranceServiceEmployeeApi from '@/api/insuranceServiceEmployeeApi'
  import {provinceAndCityData, CodeToText, TextToCode} from 'element-china-area-data'

  export default {
    created() {
      this.initData()
    },
    data() {
      return {
        baseUrl: process.env.IMG_BASE_URL,
        dialogImgFormVisible: false,
        imgUrl: '',
        dialogsFormVisible: false,
        formsData: {
          templateId: ''
        },
        allTemplateData: [],
        allDriverData: [],
        options: provinceAndCityData,
        selectedOptions: [],
        tableData: [],
        currentPage: 1,
        pageSize: 10,
        total: 0,
        tableSearchParam: {
          companyName: ''
        },
        dialogManagerFormVisible: false,
        formManagerData: {
          companyId: '',
          newManagerId: '',
          telphone: ''
        },
        dialogFormVisible: false,
        formLabelWidth: '70px',
        formData: {
          companyName: '',
          employeeName: '',
          telphone: '',
          region: '',
          address: '',
          businessLicense: {},
          companyLogo: {},
          idCards: [],
          applicants: {}

        },
        loading: false,
        actionType: '',
        multipleSelection: [],
        rules: {
          name: [
            {required: true, message: '请输入平台名称', trigger: 'blur'},
            {max: 50, message: '长度不超过50 个字符', trigger: 'blur'}
          ]
        },
        rulesManager: {
          newManagerId: [
            {required: true, message: '请选择负责人', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      showImg(url) {
        this.dialogImgFormVisible = true
        this.imgUrl = url
      },
      handleChange(value) {
        console.log(value)
        console.log(this.selectedOptions)
      },
      initData() {
        this.listTableData()
      },
      listTableData() {
        if (this.p) {
          insuranceServiceSupplierApi.listOperationRescueCompany(this.tableSearchParam, this.currentPage, this.pageSize, (res) => {
            this.tableData = res.list
            this.total = res.total
          }, () => {
          })
        } else {
          insuranceServiceSupplierApi.listRescueCompany(this.tableSearchParam, this.currentPage, this.pageSize, (res) => {
            this.tableData = res.list
            this.total = res.total
          }, () => {
          })
        }
      },
      mergeData() {

      },
      handleMerge() {

      },
      editData(index, row) {
        //清空历史值
        this.formData = {
          companyName: '',
          employeeName: '',
          telphone: '',
          region: '',
          businessLicense: {},
          companyLogo: {},
          idCards: [],
          applicants: {},
          address: ''
        }
        let that = this
        that.actionType = 'edit'
        this.loading = true
        insuranceServiceSupplierApi.getRescueCompanyById(row.id, (res1) => {
          that.formData = res1
          that.formData.id = row.id
          const sh = TextToCode[this.formData.region.split(',')[0]]
          const s = TextToCode[this.formData.region.split(',')[0]][this.formData.region.split(',')[1]]
          this.selectedOptions.push(sh.code)
          if (typeof(s) !== 'undefined') {
            this.selectedOptions.push(s.code)
          }
          this.loading = false
          that.dialogFormVisible = true
        }, () => {

        })
      },
      handleEdit() {
        this.$refs['ruleForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.formData.region = CodeToText[this.selectedOptions[0]] + "," + CodeToText[this.selectedOptions[1]]
            insuranceServiceSupplierApi.updateBaseRescueCompany(this.formData.id, this.formData, (res) => {
              this.loading = false
              this.dialogFormVisible = false
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.initData()
            }, () => {
              this.loading = false
            })
          }
        })
      },
      editManager(index, row) {
        insuranceServiceEmployeeApi.listAllRescueDriver(row.id, (res) => {
          this.allDriverData = res
          this.formManagerData.companyId = row.id
          this.dialogManagerFormVisible = true
        }, () => {
        })
      },
      handleManager() {
        this.loading = true
        insuranceServiceEmployeeApi.updateRescueCompanyManager(this.formManagerData, (res) => {
          this.loading = false
          this.dialogManagerFormVisible = false
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.initData()
        }, () => {
          this.loading = false
        })
      },
      handleDelete(index, row) {
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          insuranceServiceSupplierApi.deleteSingleRescueCompany(row.id, (res) => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.initData()
          }, () => {
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.listTableData()
      },
      handleCurrentChange(val) {
        this.currentPage = val
        this.listTableData()
      },
      handleSelectionChange(val) {
        this.multipleSelection = []
        for (var ms of val) {
          this.multipleSelection.push(ms.id)
        }
      },
      deleteAny() {
        if (this.multipleSelection.length == 0) {
          this.$message({
            type: 'warning',
            message: '请选择记录!'
          })
          return;
        }
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          insuranceServiceSupplierApi.removeRescueCompany(this.multipleSelection, (res) => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.initData()
          }, () => {
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      managerSelectChange(value) {
        for (var d of this.allDriverData) {
          if (d.id == value) {
            this.formManagerData.telphone = d.telphone
            break
          }
        }
      }
    }
  }
</script>
